home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
UNIXTOOL
/
GNU
/
GNUDBM
/
GNUDBM~
/
<tarZ$use>
/
files
/
GNUDbm
/
C
/
Test
< prev
next >
Wrap
Text File
|
1990-03-01
|
8KB
|
291 lines
/* test.c - Driver program to test the database routines.
Uses inside information. */
/* GNU DBM - DataBase Manager (database subroutines) by Philip A. Nelson
Copyright (C) 1989 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
You may contact the author by:
e-mail: phil@wwu.edu
us-mail: Philip A. Nelson
Computer Science Department
Western Washington University
Bellingham, WA 98226
phone: (206) 676-3035
*************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "gdbm.h"
#include "extern.h"
#include "gdbmerrno.h"
#include "gdbmutils.h"
/* Debug procedure to print the contents of the current hash bucket. */
void print_bucket(hash_bucket * bucket, char *mesg)
{
int index;
printf("******* %s **********\n\nbits = %d\ncount= %d\nHash Table:\n",
mesg, bucket->bucket_bits, bucket->count);
printf(" # hash value key size data size home\n");
for (index = 0; index < _dbm_file->header.bucket_elems; index++)
printf(" %4d %12x %12d %12d %5d\n", index,
bucket->h_table[index].hash_value,
bucket->h_table[index].key_size,
bucket->h_table[index].data_size,
bucket->h_table[index].hash_value % _dbm_file->header.bucket_elems);
}
/* The test program allows one to call all the routines plus the hash function.
The commands are single letter commands. The user is prompted for all other
information. The commands are q (quit), f (fetch), s (store), d (delete),
1 (firstkey), n (nextkey) and h (hash function). */
int main(void)
{
char cmd_ch;
datum key_data;
datum data_data;
datum return_data;
char key_line[500];
char data_line[1000];
char done = FALSE;
key_data.dptr = key_line;
data_data.dptr = data_line;
setbuf(stderr, 0);
_dbm_file = gdbm_open("JunkDbm", 512, DBM_WRITER, NULL);
if (_dbm_file == NULL)
{
if (gdbm_errno != CANT_BE_WRITER)
printf("dbminit failed (error %d).\n", gdbm_errno);
else
printf("Can't open as a writer. \n");
exit(2);
}
gdbm_recovery(_dbm_file, FALSE);
while (!done)
{
printf("com -> ");
cmd_ch = getchar();
while (getchar() != '\n') /* Do nothing. */ ;
switch (cmd_ch)
{
case 'h':
printf("key -> ");
gets(key_line);
key_data.dsize = strlen(key_line) + 1;
printf("hash value = %x. \n\n", _dbm_hash(key_data));
break;
case 'q':
done = TRUE;
break;
case 'f':
printf("key -> ");
gets(key_line);
key_data.dsize = strlen(key_line) + 1;
return_data = fetch(key_data);
if (return_data.dptr != NULL)
printf("data is ->%s\n\n", return_data.dptr);
else
printf("No such item found.\n\n");
break;
case 's':
printf("key -> ");
gets(key_line);
key_data.dsize = strlen(key_line) + 1;
printf("data -> ");
gets(data_line);
data_data.dsize = strlen(data_line) + 1;
if (store(key_data, data_data) != 0)
printf("Item not inserted. \n");
printf("\n");
break;
case 'd':
printf("key -> ");
gets(key_line);
key_data.dsize = strlen(key_line) + 1;
if (delete(key_data) != 0)
printf("Item not found or deleted\n");
printf("\n");
break;
case '1':
return_data = firstkey();
if (return_data.dptr != NULL)
printf("data is ->%s\n\n", return_data.dptr);
else
printf("No such item found.\n\n");
break;
case 'n':
printf("key -> ");
gets(key_line);
key_data.dsize = strlen(key_line) + 1;
return_data = nextkey(key_data);
if (return_data.dptr != NULL)
printf("data is ->%s\n\n", return_data.dptr);
else
printf("No such item found.\n\n");
break;
case '2':
strcpy(key_data.dptr, return_data.dptr);
key_data.dsize = strlen(key_line) + 1;
return_data = nextkey(key_data);
if (return_data.dptr != NULL)
printf("data is ->%s\n\n", return_data.dptr);
else
printf("No such item found.\n\n");
break;
case 'b':
{
int temp;
char number[80];
printf("bucket? ");
gets(number);
sscanf(number, "%d", &temp);
if (temp >= _dbm_file->header.dir_size / 4)
{
printf("Not a bucket. \n\n");
break;
}
_dbm_get_bucket(_dbm_file, temp << (31 - _dbm_file->header.dir_bits));
}
printf("Your bucket is now ");
case 'p':
print_bucket(_dbm_file->bucket, "Current bucket");
printf("\n current directory entry = %d.\n", _dbm_file->bucket_dir);
printf(" current bucket address = %d.\n\n", _dbm_file->bucket_adr);
break;
case 'l':
{
int temp;
printf("\nFile Header: \n\n");
printf(" table = %d\n", _dbm_file->header.dir);
printf(" table size = %d\n", _dbm_file->header.dir_size);
printf(" table bits = %d\n", _dbm_file->header.dir_bits);
printf(" file size = %d\n", _dbm_file->header.file_size);
printf(" block size = %d\n", _dbm_file->header.block_size);
printf(" bucket elems = %d\n", _dbm_file->header.bucket_elems);
printf(" bucket size = %d\n", _dbm_file->header.bucket_size);
for (temp = 0; temp < AVAIL_SIZE; temp++)
printf(" avail list[%2d] = %d\n", temp,
_dbm_file->header.avail_list[temp]);
printf(" header magic = %x\n", _dbm_file->header.header_magic);
printf(" update state = %x\n", _dbm_file->header.update_state);
printf("\n");
}
break;
case 'a':
_dbm_print_avail_list(_dbm_file);
printf("\n");
break;
case 'r':
printf("Hash table directory.\n");
printf(" Size = %d. Bits = %d. \n\n", _dbm_file->header.dir_size,
_dbm_file->header.dir_bits);
{
int temp;
for (temp = 0; temp < _dbm_file->header.dir_size / 4; temp++)
printf(" %10d: %12d\n", temp, _dbm_file->dir[temp]);
}
printf("\n");
break;
case 'c':
{
int temp;
temp = 0;
return_data = firstkey();
while (return_data.dptr != NULL)
{
temp++;
strcpy(key_data.dptr, return_data.dptr);
key_data.dsize = strlen(key_line) + 1;
return_data = nextkey(key_data);
}
printf("There are %d items in the database.\n", temp);
}
break;
case 'x':
{
if (gdbm_reorganize(_dbm_file) != 0)
printf("Reorganization failed. \n");
else
printf("Reorganization succeeded. \n");
}
break;
case 'v':
printf("%s\n", gdbm_version);
break;
case '?':
printf("a - print avail list\n");
printf("b - get and print current bucket n\n");
printf("c - count (number of entries)\n");
printf("d - delete\n");
printf("f - fetch\n");
printf("h - hash value of key\n");
printf("l - print file header\n");
printf("n - nextkey\n");
printf("p - print current bucket\n");
printf("q - quit\n");
printf("r - print hash directory\n");
printf("s - store\n");
printf("v - print version of gdbm\n");
printf("x - reorganize\n");
printf("1 - firstkey\n");
printf("2 - nextkey on last return value\n");
break;
default:
printf("What? \n\n");
break;
}
}
/* Close the database */
dbmclose();
/* Quit normally. */
return 0;
}